package io.renren.modules.wx.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.util.Json;
import io.renren.modules.wx.entity.BankQuestionEntity;
import io.renren.modules.wx.service.BankQuestionService;
import io.renren.modules.wx.vo.QuestionAndAnswerVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.wx.dao.TestDao;
import io.renren.modules.wx.entity.TestEntity;
import io.renren.modules.wx.service.TestService;


@Service("testService")
public class TestServiceImpl extends ServiceImpl<TestDao, TestEntity> implements TestService {



    @Autowired
    TestDao testDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TestEntity> page = this.page(
                new Query<TestEntity>().getPage(params),
                new QueryWrapper<TestEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void submitTest(Map<String, Object> params) {
        Integer studentId = Integer.valueOf(String.valueOf(params.get("studentId")));
        Integer testId = Integer.valueOf(String.valueOf(params.get("testId")));
        List<Integer> id =  testDao.getRecord(studentId,testId);
        if(id.size() < 1 ) {
            JSONObject obj = JSON.parseObject(params.get("answer").toString());
            obj.forEach((key, value) -> {
                testDao.insetToTestRecord(studentId, Integer.valueOf(key), Integer.valueOf(String.valueOf(value)));
            });
            testDao.insetRecord(studentId, testId);
        }
    }

    @Override
    public List<TestEntity> listByStudentId(Integer studentId) {
        List<Integer> ids = testDao.getTestIdsByStudentId(studentId);
        List<TestEntity> testEntities = null;
        if (ids.size() > 0)
            testEntities = baseMapper.selectBatchIds(ids);
        return testEntities;
    }

    @Override
    public List<QuestionAndAnswerVo> getQuestionAndAnswer(Map<String, Object> params) {
        Integer studentId = Integer.valueOf(String.valueOf(params.get("studentId")));
        Integer testId = Integer.valueOf(String.valueOf(params.get("testId")));
        List<QuestionAndAnswerVo> questionAndAnswer = this.baseMapper.getQuestionAndAnswer(studentId, testId);
        return questionAndAnswer;
    }



}
